
package com.afd.ozblip.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.afd.ozblip.activity.R;
import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.helper.BusinessVH;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.LocationUtil;
import com.afd.ozblip.util.StringUtil;

public class BusinessList3Adapter extends BaseAdapter {

    public List<BusinessEntity> dataList;
    private LayoutInflater inflater;
    private Context context;
    private double mLongitude;
    private double mLatitude;

    public BusinessList3Adapter(Context paramContext, double longitude, double latitude) {
        context = paramContext;
        this.dataList = new ArrayList<BusinessEntity>();
        this.inflater = LayoutInflater.from(context);
        this.mLongitude = longitude;
        this.mLatitude = latitude;

    }

    /**
     * 获取列表条数
     */
    @Override
    public int getCount() {
        if (CollectionUtil.UnNullAndZero(this.dataList)) {
            return this.dataList.size();
        } else {
            return 0;
        }

    }

    @Override
    public Object getItem(int paramInt) {
        if (CollectionUtil.UnNullAndZero(this.dataList)) {
            return this.dataList.get(paramInt);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int paramInt) {
        return paramInt;
    }

    @Override
    public View getView(int position, View paramView, ViewGroup paramViewGroup) {
        try {
            BusinessVH businesVH = null;
            if (paramView == null) {
                businesVH = new BusinessVH();
                paramView = this.inflater.inflate(
                        R.layout.coupon_business_item, null);
                businesVH.businessNameTv = (TextView) paramView
                        .findViewById(R.id.tv_business_item3_businessName);
                businesVH.businessAreaTv = (TextView) paramView
                        .findViewById(R.id.tv_business_item3_businessNear);
                businesVH.businessAddressTv = (TextView) paramView
                        .findViewById(R.id.tv_business_item3_businessAddress);
                businesVH.businessDistanceTv = (TextView) paramView
                        .findViewById(R.id.tv_business_item3_distance);
                paramView.setTag(businesVH);
            } else {
                businesVH = (BusinessVH) paramView.getTag();
            }

            BusinessEntity business = this.dataList.get(position);
            if (business != null) {
                // 商家名称
                final String sellerName = business.getSellerName();
                if (!StringUtil.isNullOrEmpty(sellerName)) {
                    businesVH.businessNameTv.setText(sellerName);
                } else {
                    businesVH.businessNameTv.setText(context.getResources()
                            .getString(R.string.no_text));
                }
                // 商家区域
                final String otherInfo = business.getOtherInfo();
                if (!StringUtil.isNullOrEmpty(otherInfo)) {
                    businesVH.businessAreaTv.setText(otherInfo);
                } else {
                    businesVH.businessAreaTv.setText(context.getResources()
                            .getString(R.string.no_text));
                }
                // 商家地址
                final String address = business.getSellerAddress();
                if (!StringUtil.isNullOrEmpty(address)) {
                    businesVH.businessAddressTv.setText(address);
                }
                // 商家距离
                final double addressDistance = LocationUtil.GetDistance(mLatitude, mLongitude,
                        Double.parseDouble(business.getSellerLongitude()),
                        Double.parseDouble(business.getSellerDimension()));
                DecimalFormat df = new DecimalFormat("#.00");
                String distanceStr = String.valueOf(df.format(addressDistance));
                if (!StringUtil.isNullOrEmpty(distanceStr)) {
                    businesVH.businessDistanceTv.setText(distanceStr);
                } else {
                    businesVH.businessDistanceTv.setText("?");
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return paramView;
    }

    /**
     * 添加多项
     */
    public void addItems(List<BusinessEntity> commentList) {
        this.dataList.addAll(commentList);
    }

    /**
     * 清除所有项
     */
    public void removeAllItem() {
        this.dataList.clear();
    }
}
